import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class LoginWindow extends JFrame implements ActionListener, KeyListener
{
	private JPanel panelUsername = new JPanel(new FlowLayout());
	private JPanel panelPassword = new JPanel(new FlowLayout());
	private JPanel panelLogin = new JPanel();

	private JTextField usernameField= new JTextField(15); 
	private JPasswordField passwordField = new JPasswordField(15);

	private JLabel logMeIn = new JLabel("Login");
	private JLabel error = new JLabel("");
	private JLabel usernameL = new JLabel("Username");
	private JLabel passwordL = new JLabel("Password");

	private JButton loginSubmit = new JButton("Submit");
	private final JButton createUser = new JButton("Need An Account?");

    public String getUser() { return usernameField.getText(); }
    
    
    public LoginWindow()
	{
		super("Login To Maneno Furaha");

		loginSubmit.addActionListener(this);
		//can use card layout too
		createUser.addActionListener(new ActionListener() { //debug. make it look nice in the future
			public void actionPerformed(ActionEvent e)
			{
				CreateUser a = new CreateUser();
				a.setVisible(true);
			}
		});

		logMeIn.setFont(new Font("arial", Font.PLAIN, 30));
		panelLogin.add(logMeIn);		

		error.setFont(new Font("arial", Font.BOLD, 12));
		error.setForeground(Color.RED);
		JPanel errorPane = new JPanel();
		errorPane.add(error);
		panelLogin.add(errorPane);

		panelUsername.add(usernameL);
		panelUsername.add(usernameField);

		panelPassword.add(passwordL);
		panelPassword.add(passwordField);

		panelLogin.setLayout(new BoxLayout(panelLogin,BoxLayout.Y_AXIS));
		panelLogin.add(panelUsername);
		panelLogin.add(panelPassword);

		JPanel loginSubmitPane = new JPanel();
		loginSubmitPane.add(loginSubmit);
		panelLogin.add(loginSubmitPane);

		JPanel createUserPane = new JPanel();
		createUserPane.add(createUser);
		panelLogin.add(createUserPane);

		usernameField.addKeyListener(this);
		//usernameField.setEditable(true);
		//usernameField.setEnabled(true);
		passwordField.addKeyListener(this);
		//passwordField.setEditable(true);
		//passwordField.setEnabled(true);
		loginSubmit.addKeyListener(this);
		
		createUser.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {}
            public void keyReleased(KeyEvent e) {}
            public void keyPressed(KeyEvent e) 
            {
                if(e.getKeyChar() == '\n')
                	createUser.doClick();
            }
        });

		this.add(panelLogin);

		//this.setLocation(400,300);
		setLocationByPlatform(true);
		this.setSize(600, 400);
		this.setResizable(false);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e)
	{

		if(e.getSource().equals(loginSubmit))
			try
		{
				if(DB.login(usernameField.getText(), new String(passwordField.getPassword()) ))
					this.dispose();
				else
				{
					error.setText("USERNAME OR PASSWORD NOT FOUND!");
					passwordField.setText("");
				}
		}catch(Exception exception)
		{
			error.setText("ERROR OCCURED WHILE LOGING IN.");
		}

	}

	public void keyPressed(KeyEvent e) 
	{	
		if(e.getKeyChar() == '\n')
				loginSubmit.doClick();
	}

	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}

}
